// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.

#include "stdafx.h"
#include "SurfaceInsp.h"
#include "ImagePane.h"


#include "Common/Display/Display_i.c"
#include "Comutil.h"
#include "atlconv.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CInfoBar

CImagePane::CImagePane()
{
}

CImagePane::~CImagePane()
{
	DeInitialize();
}

BOOL CImagePane::DeInitialize()
{
	if (m_pIDraw)
	{
		m_pIDraw->Release();
		m_pIDraw = NULL;
	}
	if (m_pIDib) 
	{
		m_pIDib->Release();
		m_pIDib = NULL;
	}
	if (m_pISync) 
	{
		m_pISync->Stop();
		m_pISync->ClearAllSyncItems();
		m_pISync->Release();
		m_pISync = NULL;
	}
	if (m_pIWindow) 
	{
		if (m_hwndDisplay)
		{
			m_pIWindow->DestroyWindow();
			m_hwndDisplay=NULL;
		}
		m_pIWindow->Release();
		m_pIWindow = NULL;
	}

return TRUE;

}

BEGIN_MESSAGE_MAP(CImagePane, CDockablePane)
	//{{AFX_MSG_MAP(CImagePane)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
	ON_WM_CREATE()
	ON_WM_SIZE()
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CImagePane message handlers

void CImagePane::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	CRect rectClient;
	GetClientRect (rectClient);

	dc.FillSolidRect (rectClient, ::GetSysColor (COLOR_3DHILIGHT));
	dc.Draw3dRect (rectClient, ::GetSysColor (COLOR_3DSHADOW),
		::GetSysColor (COLOR_3DLIGHT));

	dc.SetBkMode (TRANSPARENT);
	dc.SetTextColor (::GetSysColor (COLOR_BTNTEXT));
	CFont* pOldFont = (CFont*) dc.SelectStockObject (DEFAULT_GUI_FONT);

	CString str = _T("Information...");
	dc.TextOut (10, 10, str);

	dc.SelectObject (pOldFont);
}

int CImagePane::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDockablePane::OnCreate(lpCreateStruct) == -1)
		return -1;

	HRESULT hResult = CoCreateInstance(CLSID_Display, NULL, CLSCTX_INPROC_SERVER, IID_IDWWindow, (void **)&m_pIWindow);
	if ( !SUCCEEDED(hResult) )		
	{
	/*	DeInitialise();*/
		return false;
	}
	// create the dib interface object
	hResult = m_pIWindow->QueryInterface(IID_IDWDIB, (void**)&m_pIDib);
	if (!SUCCEEDED(hResult)) 
	{
		//DeInitialise();
		return false;
	}
	// create the draw interface object
	hResult = m_pIWindow->QueryInterface(IID_IDWDraw, (void**)&m_pIDraw);
	if (!SUCCEEDED(hResult)) 
	{
		//DeInitialise();
		return false;
	}
	// create the draw interface object
	hResult = m_pIWindow->QueryInterface(IID_IDWSynchronizer, (void**)&m_pISync);
	if (!SUCCEEDED(hResult)) 
	{
		//DeInitialise();
		return false;
	}
	// create the actual window
	hResult = m_pIWindow->Create( m_hWnd, OLESTR("Display Window"), &m_hwndDisplay );
	if ( !SUCCEEDED(hResult) )
	{
		//DeInitialise();
		return false;
	}

	RECT	rect;
	GetClientRect(&rect);
	::MoveWindow(m_hwndDisplay,0,0,rect.right-rect.left,rect.bottom-rect.top,TRUE);


	// TODO:  Add your specialized creation code here

	return 0;
}

void CImagePane::OnSize(UINT nType, int cx, int cy)
{
	CDockablePane::OnSize(nType, cx, cy);

	RECT	rect;
	GetClientRect(&rect);
	::MoveWindow(m_hwndDisplay,0,0,rect.right-rect.left,rect.bottom-rect.top,TRUE);

	if (m_hWnd) 
		Invalidate();
	if(m_pIWindow)
	{
		m_pIWindow->InvalidateWindow();
		m_pIWindow->UpdateWindow();
		m_pIWindow->ImageChanged();
	}

}

BOOL CImagePane::ShowImage( CString strImg)
{

	if (m_pIDib==NULL || m_pIWindow==NULL ) 
	{
		m_pActiveImg=NULL;
		return  FALSE;
	}
	if (!strImg.IsEmpty() && m_pActiveImg)
	{
		BSTR strimg = strImg.AllocSysString();
		//if (!m_bStreaming) 
		m_pIDib->Load(strimg,DIB_LOADGRAPHICS);
		SysFreeString(strimg);
		m_pIDib->GetDimensions(&m_lImgWidth,&m_lImgHeight);
		//update resolution
		/*	m_dvRes=0.0;
		m_dhRes=0.0;
		m_dhRes = m_pActiveImg->GetHorizontalResolution();
		m_dvRes = m_pActiveImg->GetVerticalResolution();*/

		//m_pActiveImg->GetResolution(m_dhRes,m_dvRes);
		//double dAvgRes = (dvRes+dhRes)/2;

		//from wafermapdll = pix/mm
		//to display dll = centipix/micron  =(*100/1000) = /10
		//			m_pIWindow->SetResolution((float)(m_dhRes/10),(float)(m_dvRes/10)); 

		if (!m_bImgLoaded)
			Invalidate();
		m_bImgLoaded=true;

	}
	else
	{
		m_pIDib->SetImageBuffer(NULL,0);
		m_lImgWidth=m_lImgHeight=0;
		if (m_bImgLoaded)
			Invalidate();
		m_bImgLoaded=false;

	}

	if(m_pIWindow)
	{
		m_pIWindow->InvalidateWindow();
		m_pIWindow->UpdateWindow();
		m_pIWindow->ImageChanged();
	}


	return TRUE;

}